package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.UserHolder;
import com.itheima.reggie.domain.Cart;
import com.itheima.reggie.mapper.CartMapper;
import com.itheima.reggie.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;


// 购物车
@Service
@Transactional
public class CartServiceImpl implements CartService {
    @Autowired
    private CartMapper cartMapper;
    //添加购物车
    @Override
    public Cart add(Cart cartParam) {
        //1.查询是否有此购物项纪录
        //select * from shopping_cart where user_id =?
        //and (dish_id=? | setmeal_id = ?)
        LambdaQueryWrapper<Cart> qw = new LambdaQueryWrapper<>();
        //1-1用户ID
        Long userId = UserHolder.get().getId();
        qw.eq(Cart::getUserId,userId);
        //1-2菜品id
        qw.eq(cartParam.getDishId()!=null,Cart::getDishId,cartParam.getDishId());
        //1-3套餐id
        qw.eq(cartParam.getSetmealId()!=null,Cart::getSetmealId,cartParam.getSetmealId());
        //1-4执行查询
        Cart cart = cartMapper.selectOne(qw);
        //判断
        if(cart!=null){
            //2.数量+1，更新数据库
            cart.setNumber(cart.getNumber()+1);
            cartMapper.updateById(cart);
        }else {
            //3.补全信息，添加数据库
            cart=cartParam;
            cart.setUserId(userId);
            cart.setNumber(1);
            cart.setCreateTime(new Date());
            cartMapper.insert(cart);
        }
        //4.返回购物车
        return cart;
    }
    //查看购物车列表
    @Override
    public List<Cart> list() {
        //1.构建条件 select * from shopping_cart where user_id=?
        LambdaQueryWrapper<Cart> qw = new LambdaQueryWrapper<>();
        qw.eq(Cart::getUserId,UserHolder.get().getId());
        //2.执行查询
        List<Cart> list = cartMapper.selectList(qw);
        return list;
    }
    //清空购物车
    @Override
    public void clean() {
        //1.构建条件
        //delete from shopping_cart where user_id =?
        LambdaQueryWrapper<Cart> qw = new LambdaQueryWrapper<>();
        qw.eq(Cart::getUserId,UserHolder.get().getId());
        cartMapper.delete(qw);
    }

    //删除购物车
    @Override
    public Cart deleteCart(Cart cartParam) {
       // myplus自带动态sql功能
        //1.查询需要修改的购物车对象
        //1.1根据用户id
        //根据dishid或setmealid
        //select * from shopping_cart where userid=? and setmeal_id=? |dish_id=?
        Long userId = UserHolder.get().getId();
        LambdaQueryWrapper<Cart> qw = new LambdaQueryWrapper<>();
        qw.eq(Cart::getUserId,userId);
        qw.eq(cartParam.getSetmealId()!=null,Cart::getSetmealId,cartParam.getSetmealId());
        qw.eq(cartParam.getDishId()!=null,Cart::getDishId,cartParam.getDishId());
        Cart cart = cartMapper.selectOne(qw);
        //2.操作购物车数量-1
        cart.setNumber(cart.getNumber()-1);
        //3.判断修改后的数量
        //如果=0，删除购物车纪录
        if(cart.getNumber()==0){
            cartMapper.deleteById(cart);
        }else {
            cartMapper.updateById(cart);
        }
        //如果>0,更新购物车纪录
        //4.返回购物车对象
        return cart;
    }
}
